package com.ruoyi.api.api;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;

/**

 代理分成计算系统
 实现多级代理按比例分成的功能
 */
public class AgentCommissionSystem {
    // 精度设置：保留两位小数
    private static final int SCALE = 2;
    // 总分成比例上限
    private static final BigDecimal TOTAL_RATIO_LIMIT = new BigDecimal ("1.00");
    // 最大代理级数
    private static final int MAX_LEVELS = 10;
    // 各级代理的固定分成比例（总和不超过 100%）
    private static final List<BigDecimal> LEVEL_RATIOS = new ArrayList<>();
    static {
// 初始化各级代理的固定分成比例
        LEVEL_RATIOS.add (new BigDecimal ("0.20")); // 1 级：20%
        LEVEL_RATIOS.add (new BigDecimal ("0.15")); // 2 级：15%
        LEVEL_RATIOS.add (new BigDecimal ("0.12")); // 3 级：12%
        LEVEL_RATIOS.add (new BigDecimal ("0.09")); // 4 级：9%
        LEVEL_RATIOS.add (new BigDecimal ("0.07")); // 5 级：7%
        LEVEL_RATIOS.add (new BigDecimal ("0.05")); // 6 级：5%
        LEVEL_RATIOS.add (new BigDecimal ("0.04")); // 7 级：4%
        LEVEL_RATIOS.add (new BigDecimal ("0.03")); // 8 级：3%
        LEVEL_RATIOS.add (new BigDecimal ("0.02")); // 9 级：2%
        LEVEL_RATIOS.add (new BigDecimal ("0.01")); // 10 级：1%
// 验证总比例不超过限制
        BigDecimal totalRatio = BigDecimal.ZERO;
        for (BigDecimal ratio : LEVEL_RATIOS) {
            totalRatio = totalRatio.add (ratio);
        }
        if (totalRatio.compareTo (TOTAL_RATIO_LIMIT) > 0) {
            throw new IllegalArgumentException ("各级代理比例之和超过 100%");
        }
    }
    /**
     计算各级代理的分成
     @param initialFunds 起始资金
     @param levelCount 要计算的代理级数
     @return 各级代理的分成金额列表
     */
    public List<BigDecimal> calculateCommissions(BigDecimal initialFunds, int levelCount) {
        List<BigDecimal> commissions = new ArrayList<>();
// 检查输入参数
        if (initialFunds == null || initialFunds.compareTo (BigDecimal.ZERO) <= 0) {
            throw new IllegalArgumentException ("起始资金必须为正数");
        }
        if (levelCount <= 0 || levelCount> MAX_LEVELS) {
            throw new IllegalArgumentException ("代理级数必须在 1 到" + MAX_LEVELS + "之间");
        }
        BigDecimal remainingFunds = initialFunds;
        BigDecimal totalCommission = BigDecimal.ZERO;
// 计算每级代理的分成
        for (int i = 0; i < levelCount; i++) {
            BigDecimal ratio = LEVEL_RATIOS.get (i);
// 最后一级代理：使用剩余资金计算，确保总额不超限
            if (i == levelCount - 1) {
                BigDecimal commission = remainingFunds.setScale (SCALE, RoundingMode.HALF_UP);
                commissions.add (commission);
                totalCommission = totalCommission.add (commission);
                break;
            }
// 非最后一级：按固定比例计算
            BigDecimal commission = initialFunds.multiply (ratio)
                    .setScale (SCALE, RoundingMode.HALF_UP);
            commissions.add (commission);
            totalCommission = totalCommission.add (commission);
            remainingFunds = remainingFunds.subtract (commission);
        }
        return commissions;
    }
    /**
     计算并展示各级代理分成
     */
    public void showCommissionDetails (BigDecimal initialFunds, int levelCount) {
        System.out.println ("===== 代理分成计算结果 =====");
        System.out.println ("起始资金:" + initialFunds.setScale (SCALE, RoundingMode.HALF_UP) + "元");
        System.out.append ("-------------------------");
        List<BigDecimal> commissions = calculateCommissions(initialFunds, levelCount);
        BigDecimal totalCommission = BigDecimal.ZERO;
        for (int i = 0; i < commissions.size (); i++) {
            BigDecimal commission = commissions.get (i);
            totalCommission = totalCommission.add (commission);
            System.out.printf ("第 % d 级代理：分成金额 = %.2f 元 (比例: %.2f%%)\n ",
                    (i + 1), commission.doubleValue (), LEVEL_RATIOS.get (i).multiply (new BigDecimal ("100")).doubleValue ());
        }
        System.out.println ("-------------------------");
        System.out.printf ("各级代理分成总额: %.2f 元 \n", totalCommission.doubleValue ());
        BigDecimal remainingFunds = initialFunds.subtract (totalCommission);
        System.out.printf ("剩余资金: %.2f 元 \n", remainingFunds.doubleValue ());
        System.out.println ("===== 计算结束 =====");
    }
    /**
     主方法：演示系统功能
     */
    public static void main (String [] args) {
// 使用更合理的起始资金：1 亿元
        BigDecimal initialFunds = new BigDecimal ("100000000");
// 计算 10 级代理
        int maxLevel = 10;
        AgentCommissionSystem system = new AgentCommissionSystem();
        system.showCommissionDetails(initialFunds, maxLevel);
    }
}
